<?php


namespace app\api\controller\integral;


use app\api\controller\Controller;
use app\api\model\integral\Category as CategoryModel;
use app\api\model\integral\Goods as GoodsModel;
use app\common\service\qrcode\Goods as GoodsPoster;

/**
 * 积分商城
 * Class Goods
 * @package app\api\controller\integral
 */
class Goods extends Controller
{
    /**
     * 商品分类
     */
    public function category()
    {
        $categories = CategoryModel::getCacheAll();
        return $this->renderSuccess(compact('categories'));
    }

    /**
     * 商品列表
     */
    public function index(GoodsModel $goodsModel)
    {
        $list = $goodsModel->getList($this->request->param(), $this->getUser(false));
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 获取商品详情
     */
    public function detail($goods_id, GoodsModel $goodsModel)
    {
        // 商品详情
        $goods = $goodsModel->getDetails($goods_id, $this->getUser(false));
        if ($goods === false) {
            return $this->renderError($goodsModel->getError() ?: '商品信息不存在');
        }
        // 多规格商品sku信息, todo: 已废弃 v1.1.25
        $specData = $goods['spec_type'] == 20 ? $goodsModel->getManySpecData($goods['spec_rel'], $goods['sku']) : null;

        return $this->renderSuccess([
            // 商品详情
            'detail' => $goods,
            // 多规格商品sku信息
            'specData' => $specData,
        ]);
    }

    /**
     * 生成商品海报
     */
    public function poster($goods_id)
    {
        // 商品详情
        $detail = GoodsModel::detail($goods_id);
        // 生成推广二维码
        $Qrcode = new GoodsPoster($detail, $this->getUser(false), 30);
        return $this->renderSuccess([
            'qrcode' => $Qrcode->getImage(),
        ]);
    }
}